package pl.edu.pw.elka.wedt.sem10Z.jslt.filesystem;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.FileType;
import org.apache.commons.vfs.VFS;
import org.apache.log4j.Logger;

import pl.edu.pw.elka.wedt.sem10Z.jslt.util.PorterStemmer;

public class FileReader {

	private final Logger LOGGER = Logger.getLogger(FileReader.class);
	
	private List<Document> documentList = new ArrayList<Document>();
	private List<WordCount> globalFrequencyList = new ArrayList<WordCount>();
	
	private PorterStemmer ps = new PorterStemmer();
	
	public FileReader() {
		// TODO Auto-generated constructor stub
	}
	
	public void readFiles(final File directory) {
		FileSystemManager fsManager;
		
		try {
			fsManager = VFS.getManager();
	
			FileObject fs = fsManager.resolveFile(directory.toString());

			FileObject[] children = fs.getChildren();
			for(int j = 0; j < children.length; j++) {
				LOGGER.debug(new StringBuffer("Reading dir: ").append(fs.getName().getURI()).toString());
				if(children[j].getName().getBaseName().contains(".")){
					continue;
				}
				
				FileObject[] subChildren = children[j].getChildren();
				for (int i = 0; i < subChildren.length; i++) {
					
					//LOGGER.debug("Reading: " + subChildren[i].getName().getBaseName());
					if(FileType.FILE.equals(subChildren[i].getType()) && subChildren[i].isReadable()) {
						String content = this.readFile(subChildren[i]);
						Document document = new Document();
						document.setContent(content);
						document.setCategory(children[j].getName().getBaseName());
						document.setName(subChildren[i].getName().getBaseName());
						document.setFrequencyList(countFrequency(content)); // brzydko, niespojnie, aczkolwiek intuicja nakazuje mi tak zrobic
						documentList.add(document);
					//	LOGGER.debug(new StringBuffer("From file (first line): ").append(content.substring(0, 10)).append("... ").toString()); 
					} else {
						LOGGER.error(new StringBuffer("Nie plik lub nieczytelny: ").append(subChildren[i].getName()).toString());
					}
				}
			}
			LOGGER.debug("Number of files: " + documentList.size());
		} catch (FileSystemException e) {
			LOGGER.error("", e);
		}
	}
	
	public String readFile(final FileObject file) {
		String content = null;
		try {
			InputStream is = file.getContent().getInputStream();
			content = IOUtils.toString(is);
		} catch (FileNotFoundException e) {
			LOGGER.error("", e);
		} catch (IOException e) {
			LOGGER.error("", e);
		}
		return content;
	}
	
	public List<WordCount> countFrequency(String content) {
		List<WordCount> list = new ArrayList<WordCount>();
		
		String [] tmp = content.split("[^\\w]|\\s+");
		
		for(int i=0; i < tmp.length; i++) {
			String word = tmp[i];
			if(word.isEmpty()) {
				continue;
			}
			try {
				if(word.length() > 2) {
					word = ps.stem(word);
					if(word.equals("Invalid term")) {
						continue;
					}
				}
			} catch (Exception e) {
				LOGGER.debug("Wyrazy dwuznakowe czasem wywracaja ten stemmer, z drugiej strony takich nie ma co stemmowac.");
			}
			word = word.toLowerCase();
			
			// local
			boolean localListContainWord = false;
			for(WordCount wc : list) {
				if(wc.getWord().equals(word)) {
					wc.setCount(wc.getCount()+1);
					localListContainWord = true;
				} 
			}
			if(!localListContainWord) {
				list.add(new WordCount(word, 1));
			}
		}
		Collections.sort(list, Collections.reverseOrder());
		

		return list;
	}
	
	public void buildGlobalFrequencyList() {
		for(Document d : documentList) {
			globalFrequencyList.addAll(d.getFrequencyList());
		}
		for(WordCount wc : globalFrequencyList) {
			String word = wc.getWord();
			if(word == null) {
				continue;
			}
			
			for(WordCount wc2 : globalFrequencyList) {
				if(word != wc2.getWord() && word.equals(wc2.getWord())){
					wc.setCount(wc.getCount() + wc2.getCount());
					wc2.setWord(null);
					wc2.setCount(0);
				}
			}
		}
		
		Collections.sort(globalFrequencyList, Collections.reverseOrder());
	}
	
	public List<Document> getDocumentList() {
		return documentList;
	}

	public void setDocumentList(List<Document> contentList) {
		this.documentList = contentList;
	}

	public void setGlobalFrequencyList(List<WordCount> globalFrequencyList) {
		this.globalFrequencyList = globalFrequencyList;
	}

	public List<WordCount> getGlobalFrequencyList() {
		return globalFrequencyList;
	}
}
